﻿Namespace Functions
    Public Class MetisFunctionList
        Inherits MetisSortedEntityList(Of MetisFunction)

        Private _MetisCore As MetisCore
        Private _Parent As IMetisCommonEntity

        ''' <summary>Creates a new FunctionList and loads all functions from the DB if specified.</summary>
        ''' <param name="MetisCore">Reference to the central management object of Metis that manages the access to the DB.</param>
        ''' <param name="LoadFromDB">Fills the List with all functions from the DB</param>
        Sub New(ByVal MetisCore As Core.MetisCore, Optional ByVal LoadFromDB As Boolean = False)
            _MetisCore = MetisCore
            If LoadFromDB Then
                Dim OraReader As OracleDataReader
                Dim OraCmd As OracleCommand
                OraCmd = New OracleCommand("SELECT DISTINCT f.namespace_fk, f.func_type, f.function_id, f.name, f. description, " & _
                                           "f.table_name, f.modified_on, f.created_on, r.lvl, f.action " & _
                                           "FROM dam_functions f, dam_function_ref_lvl r " & _
                                           "WHERE f.function_id=r.function_id", _MetisCore.Connection)

                OraReader = OraCmd.ExecuteReader
                Do While OraReader.Read
                    Dim tmpNamespace As MetisNamespace = If(Not OraReader.IsDBNull(0), _MetisCore.Namespaces.Find(OraReader.GetInt32(0)), Nothing)

                    Dim tmpAnnotation As String = If(Not OraReader.IsDBNull(4), OraReader.GetString(4), "")

                    Dim tmpFunc As MetisFunction = MetisFunction.FunctionFactory(_MetisCore, tmpNamespace, OraReader.GetInt32(1), _
                                                                                 OraReader.GetInt32(2), OraReader.GetString(3), _
                                                                                 tmpAnnotation, _MetisCore.DBTables.FindTable(OraReader.GetString(5)), _
                                                                                 OraReader.GetDateTime(6), OraReader.GetDateTime(7), _
                                                                                 OraReader.GetInt32(8), OraReader.GetInt32(9))
                    Me.Add(tmpFunc)
                    If tmpNamespace IsNot Nothing Then
                        tmpNamespace.Children.Add(tmpFunc)
                    Else
                        _MetisCore.Namespaces.Add(tmpFunc)
                    End If
                Loop
                OraReader.Close()
            End If

        End Sub

        ''' <summary>Creates a new FunctionList</summary>
        ''' <param name="MetisCore">Reference to the central management object of Metis that manages the access to the DB.</param>
        ''' <param name="Parent">Reference to a dimension, flow, function or namespace that holds this list</param>
        ''' <remarks>No further action (load from DB) will be taken.</remarks>
        Sub New(ByVal MetisCore As Core.MetisCore, ByVal Parent As IMetisCommonEntity)
            _MetisCore = MetisCore
            _Parent = Parent
        End Sub

        Public Overrides Sub Reload()
            Dim tmpFuncList As New MetisFunctionList(_MetisCore)
            For i As Integer = Me.Count - 1 To 0 Step -1
                If Me(i).ID >= 0 AndAlso tmpFuncList.Find(Me(i).ID) Is Nothing Then
                    Me.RemoveAt(i)
                End If
            Next
            For Each Item As MetisFunction In tmpFuncList
                Dim tmpFunc As MetisFunction = Me.Find(Item.ID)
                If tmpFunc Is Nothing Then
                    Me.Add(Item)
                ElseIf tmpFunc.LoadState = MetisLoadState.Loaded Then
                    tmpFunc.Reload()
                End If
            Next
        End Sub

        Public ReadOnly Property Parent() As IMetisCommonEntity
            Get
                Return _Parent
            End Get
        End Property

    End Class
End Namespace

